<template>
    <rx-dialog @handOk="onOk" @cancel="cancel" btnalign="center" order="bottom">
        <rx-layout>
            <div slot="center" style="margin:10px">
                <div style="text-align:left;padding:5px;">
                    <a-button-group>
                        <a-button type="primary" @click="expandAll" size="small">{{i18n('transfer.expansionBtn','展开')}}</a-button>
                        <a-button type="primary" @click="collapseAll" size="small">{{i18n('transfer.putAwayBtn','收起')}}</a-button>
                    </a-button-group>
                </div>
                <rx-tree ref="rxTree" :treeData="sysTreeList" :showTreeIcon="true" :multiSelect="multiSelect"/>
            </div>
        </rx-layout>
    </rx-dialog>
</template>

<script>
import {RxDialog, Util, RxLayout, RxTree} from "@lowcode/jpaas-common-lib"


export default {
    name: "SysTreeChange",
    props: {
        doctype: {
            type: String,
            default: ""
        },
        layerid: {
            type: String,
            default: ""
        },
        destroy: {
            type: Function
        },
        multiSelect: {
            type: Boolean
        },
        sysTreeData: {
            type: Array
        }
    },
    components: {
        RxDialog,
        RxLayout,
        RxTree
    },
    computed: {
        rxTree() {
            return this.$refs.rxTree;
        }
    },
    data() {
        return {
            sysTreeList: [],
        }
    },
    created() {
        this.sysTreeList = this.sysTreeData.map(item => {
            return {
                "name":item.name,
                "id":item.treeId,
                "treeId":item.treeId,
                "path":item.path,
                "parentId":item.parentId,
            }
        });
        let topTree = {
            "name":this.i18n('transfer.topClass','顶级分类'),
            "id":"0",
            "treeId":"0",
            "path":"0",
            "parentId":"root",
        }
        this.sysTreeList.push(topTree);
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"SysTree",key);
        },
        onOk(e) {
            let selectTree = this.getSelectTree();
            if (!selectTree || selectTree.length == 0) {
                this.$message.error(this.i18n('transfer.selectTips','请选择分类！'));
                return;
            }
            Util.closeWindow(this, "ok");
        },
        cancel() {
            Util.closeWindow(this, "cancel");
        },
        collapseAll() {
            this.rxTree.collapseAll();
        },
        expandAll() {
            this.rxTree.expandAll();
        },
        getSelectTree() {
          let selectedKeys = this.rxTree.selectedKeys
            if(this.multiSelect){
                return selectedKeys;
            }
            let obj = this.sysTreeList.find(item=>item.treeId === selectedKeys[0])
            if(obj){
              return [obj]
            }
            return this.rxTree.getCheckedNodes(true);
        }
    }
}
</script>

<style scoped>

</style>